<?php

class ProductAction extends UserAction
{
    public $token;
    public $product_model;
    public $product_cat_model;
    public $isDining;

    public function _initialize()
    {
        parent::_initialize();
        //
        $token_open = M('token_open')->field('queryname')->where(array('token' => session('token')))->find();
        if ((!isset($_GET['dining']) && !strpos($token_open['queryname'], 'shop')) || (isset($_GET['dining']) && !strpos($token_open['queryname'], 'dx')))
        {
            // $this->error('您还未开启该模块的使用权,请到功能模块中添加',U('Function/index',array('token'=>session('token'),'id'=>session('wxid'))));
        }
        //是否是餐饮
        if (isset($_GET['dining']) && intval($_GET['dining']))
        {
            $this->isDining = 1;
        } else
        {
            $this->isDining = 0;
        }
        $this->assign('isDining', $this->isDining);
    }

    public function index()
    {
        $catid = intval($_GET['catid']);
        $catid = $catid == '' ? 0 : $catid;
        $product_model = M('Product');
        $product_cat_model = M('Product_cat');
        $where = array('token' => session('token'));
        if ($catid)
        {
            $where['catid'] = $catid;
        }
        $where['dining'] = $this->isDining;
        $where['groupon'] = array('neq', 1);
        if (IS_POST)
        {
            $key = $this->_post('searchkey');
            if (empty($key))
            {
                $this->error("关键词不能为空");
            }

            $map['token'] = $this->get('token');
            $map['name|intro|keyword'] = array('like', "%$key%");
            $list = $product_model->where($map)->select();
            $count = $product_model->where($map)->count();
            $Page = new Page($count, 20);
            $show = $Page->show();
        } else
        {
            $count = $product_model->where($where)->count();
            $Page = new Page($count, 20);
            $show = $Page->show();
            $list = $product_model->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        }
        $this->assign('page', $show);
        $this->assign('list', $list);
        $this->assign('isProductPage', 1);

        $this->display();
    }

    public function sums2()
    {
        $name_ar = array();
        $pronames = M("product")->field(" id,name ")->select();
        foreach ($pronames as $piece)
        {
            $name_ar[$piece["id"]] = $piece["name"];
        }


        $this->assign("name_ar", $name_ar);
        $product_cat_model = M('product_cart');
        $where = array('token' => session('token'));
        $where['_string'] = "UNIX_TIMESTAMP()-time<86400";


        $list = $product_cat_model->where($where)->select();
        //echo $product_cat_model->getLastSql();


        $sumdata = M("sumdata");
        $sumdata->query("truncate table sumdata");
        foreach ($list as $order)
        {
            $products = unserialize($order['info']);
            //dump($products);
            foreach ($products as $key => $val)
            {
                $data = array();
                $data["pid"] = $key;
                $data["pcount"] = $val["count"];
                $data["pprice"] = $val["price"];
                $data["adddate"] = date("Y-m-d", $order["time"]);
                $data["tel"] = $order["tel"];
                $data["truename"] = $order["truename"];
                $data["handled"] = $order["handled"];
                $sumdata->add($data);
            }
        }

        $sqlx = "select pid,sum(pcount) as tp_count from tp_sumdata where adddate='" . date("Y-m-d") . "'  group by pid ";
        $custlist = $product_cat_model->query($sqlx);
        $this->assign('list', $custlist);

        $sqlx1 = "select pid,sum(pcount) as tp_count from tp_sumdata ";
        $sqlx1 .= "where adddate='" . date("Y-m-d") . "' and handled=0  group by pid ";
        $custlist1 = $product_cat_model->query($sqlx1);
        $this->assign('list1', $custlist1);

        $this->display();
    }

    public function sums()
    {

        $name_ar = array();
        $pronames = M("product")->field(" id,name ")->select();

        foreach ($pronames as $piece)
        {
            $name_ar[$piece["id"]] = $piece["name"];
        }


        $this->assign("name_ar", $name_ar);


        $product_cat_model = M('product_cart');

        $where = array('token' => session('token'));
        //        $thisday=strtotime(date("Y-m-d" ));
        // $thisday=strtotime("2014-08-06");
        //  $where["time"]=array('between',array( $thisday  , $thisday+86400));
        $where["sumed"] = array("neq", 1);
        $list = $product_cat_model->where($where)->select();
        echo $product_cat_model->getLastSql();


        $sumdata = M("sumdata");

        foreach ($list as $order)
        {
            $products = unserialize($order['info']);
            dump($products);

            foreach ($products as $key => $val)
            {


                $data = array();
                $data["pid"] = $key;
                $data["pcount"] = $val["count"];
                $data["pprice"] = $val["price"];
                $data["adddate"] = date("Y-m-d", $order["time"]);
                $data["tel"] = $order["tel"];
                $data["truename"] = $order["truename"];
                $sumdata->add($data);

            }
            $product_cat_model->where("id=" . $order["id"])->setField("sumed", 1);
            echo $product_cat_model->getLastSql();
        }

        $sumlist = $product_cat_model->query("select pid, sum(pcount) as tp_count ,sum(pprice) as tp_price,adddate from tp_sumdata  where adddate='2014-09-05'
group by pid ");
        // dump($sumlist);

        $sqlx = "select pid, tel,truename,  sum(pprice) as tp_price,adddate  from tp_sumdata";
        $sqlx .= "     group by tel ,adddate order by adddate desc ";
        $custlist = $product_cat_model->query($sqlx);
        $this->assign('custlist', $custlist);

        $this->display();
    }


    public function cats()
    {
        /*
        $token_open=M('token_open')->field('queryname')->where(array('token'=>session('token')))->find();

        if(!strpos($token_open['queryname'],'adma')){
            $this->error('您还未开启该模块的使用权,请到功能模块中添加',U('Function/index',array('token'=>session('token'),'id'=>session('wxid'))));}
         */
        $parentid = intval($_GET['parentid']);
        $parentid = $parentid == '' ? 0 : $parentid;
        $data = M('Product_cat');
        $where = array('token' => session('token'));
        $where['dining'] = $this->isDining;
        if (IS_POST)
        {
            $key = $this->_post('searchkey');
            if (empty($key))
            {
                $this->error("关键词不能为空");
            }

            $map['token'] = $this->get('token');
            $map['name|des'] = array('like', "%$key%");
            $list = $data->where($map)->select();
            $count = $data->where($map)->count();
            $Page = new Page($count, 20);
            $show = $Page->show();
        } else
        {
            $count = $data->where($where)->count();
            $Page = new Page($count, 20);
            $show = $Page->show();
            $list = $data->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        }
        $this->assign('page', $show);
        $this->assign('list', $list);
        if ($parentid)
        {
            $parentCat = $data->where(array('id' => $parentid))->find();
        }
        $this->assign('parentCat', $parentCat);
        $this->assign('parentid', $parentid);
        $this->display();
    }

    public function catAdd()
    {
        if (IS_POST)
        {

            if ($this->isDining)
            {
                $this->insert('Product_cat', '/cats?dining=1&parentid=' . $this->_post('parentid'));
            } else
            {
                $this->insert('Product_cat', '/cats?parentid=' . $this->_post('parentid'));
            }
        } else
        {
            $parentid = intval($_GET['parentid']);
            $parentid = $parentid == '' ? 0 : $parentid;
            $this->assign('parentid', $parentid);

            $pname = D('product_cat')->where(array('token' => $this->get('token')))->select();
            $this->assign('parentidlist', $pname);


            $this->display('catSet');
        }
    }

    public function catDel()
    {
        if ($this->_get('token') != session('token'))
        {
            $this->error('非法操作');
        }
        $id = $this->_get('id');
        if (IS_GET)
        {
            $where = array('id' => $id, 'token' => session('token'));
            $data = M('Product_cat');
            $check = $data->where($where)->find();
            if ($check == false) $this->error('非法操作');
            $product_model = M('Product');
            $productsOfCat = $product_model->where(array('catid' => $id))->select;
            if (count($productsOfCat))
            {
                $this->error('本分类下有商品，请删除商品后再删除分类', U('Product/cats', array('token' => session('token'), 'dining' => $this->isDining)));
            }
            $back = $data->where($wehre)->delete();
            if ($back == true)
            {
                if (!$this->isDining)
                {
                    $this->success('操作成功', U('Product/cats', array('token' => session('token'), 'parentid' => $check['parentid'])));
                } else
                {
                    $this->success('操作成功', U('Product/cats', array('token' => session('token'), 'parentid' => $check['parentid'], 'dining' => 1)));
                }
            } else
            {
                $this->error('服务器繁忙,请稍后再试', U('Product/cats', array('token' => session('token'))));
            }
        }
    }

    public function catSet()
    {
        $id = $this->_get('id');
        $checkdata = M('Product_cat')->where(array('id' => $id))->find();
        if (empty($checkdata))
        {
            $this->error("没有相应记录.您现在可以添加.", U('Product/catAdd'));
        }
        if (IS_POST)
        {
            $data = D('Product_cat');
            $where = array('id' => $this->_post('id'), 'token' => session('token'));
            $check = $data->where($where)->find();
            if ($check == false) $this->error('非法操作');
            if ($data->create())
            {
                if ($data->where($where)->save($_POST))
                {
                    if (!$this->isDining)
                    {
                        $this->success('修改成功', U('Product/cats', array('token' => session('token'), 'parentid' => $this->_post('parentid'))));
                    } else
                    {
                        $this->success('修改成功', U('Product/cats', array('token' => session('token'), 'parentid' => $this->_post('parentid'), 'dining' => 1)));
                    }

                } else
                {
                    $this->error('操作失败');
                }
            } else
            {
                $this->error($data->getError());
            }
        } else
        {
            $pname = D('product_cat')->where(array('token' => $this->get('token')))->select();
            $this->assign('parentid', $pname);
            $this->assign('set', $checkdata);
            $this->display();

        }
    }

    public function add()
    {
        if (IS_POST)
        {
            $this->all_insert('Product', '/index?token=' . session('token') . '&dining=' . $this->isDining);
        } else
        {
            //分类
            $data = M('Product_cat');
            $catWhere = array('parentid' => 0, 'token' => session('token'));
            if ($this->isDining)
            {
                $catWhere['dining'] = 1;
            } else
            {
                $catWhere['dining'] = 0;
            }
            $cats = $data->where($catWhere)->select();
            if (!$cats)
            {
                $this->error("请先添加分类", U('Product/catAdd', array('token' => session('token'), 'dining' => $this->isDining)));
                exit();
            }
            $this->assign('cats', $cats);
            $catsOptions = $this->catOptions($cats, 0);
            $this->assign('catsOptions', $catsOptions);
            //
            $this->assign('isProductPage', 1);

            $pname = D('product_cat')->where(array('token' => $this->get('token')))->select();
            $this->assign('parentidlist', $pname);

            $this->display('set');
        }
    }

    /**
     * 商品类别ajax select
     *
     */
    public function ajaxCatOptions()
    {
        $parentid = intval($_GET['parentid']);
        $data = M('Product_cat');
        $catWhere = array('parentid' => $parentid, 'token' => session('token'));
        $cats = $data->where($catWhere)->select();
        $str = '';
        if ($cats)
        {
            foreach ($cats as $c)
            {
                $str .= '<option value="' . $c['id'] . '">' . $c['name'] . '</option>';
            }
        }
        $this->show($str);
    }

    public function set()
    {
        $id = $this->_get('id');
        $product_model = M('Product');
        $product_cat_model = M('Product_cat');
        $checkdata = $product_model->where(array('id' => $id))->find();
        if (empty($checkdata))
        {
            $this->error("没有相应记录.您现在可以添加.", U('Product/add'));
        }
        if (IS_POST)
        {
            $where = array('id' => $this->_post('id'), 'token' => session('token'));
            $check = $product_model->where($where)->find();
            if ($check == false) $this->error('非法操作');
            if ($product_model->create())
            {
                if ($product_model->where($where)->save($_POST))
                {
                    $this->success('修改成功', U('Product/index', array('token' => session('token'), 'dining' => $this->isDining)));
                    $keyword_model = M('Keyword');
                    $keyword_model->where(array('token' => session('token'), 'pid' => $this->_post('id'), 'module' => 'Product'))->save(array('keyword' => $this->_post('keyword')));
                } else
                {
                    $this->error('操作失败');
                }
            } else
            {
                $this->error($product_model->getError());
            }
        } else
        {
            //分类
            $catWhere = array('parentid' => 0, 'token' => session('token'));
            if ($this->isDining)
            {
                $catWhere['dining'] = 1;
            }
            $cats = $product_cat_model->where($catWhere)->select();
            $this->assign('cats', $cats);

            $thisCat = $product_cat_model->where(array('id' => $checkdata['catid']))->find();
            $childCats = $product_cat_model->where(array('parentid' => $thisCat['parentid']))->select();
            $this->assign('thisCat', $thisCat);
            $this->assign('parentCatid', $thisCat['parentid']);
            $this->assign('childCats', $childCats);
            $this->assign('isUpdate', 1);
            $catsOptions = $this->catOptions($cats, $checkdata['catid']);
            $childCatsOptions = $this->catOptions($childCats, $thisCat['id']);
            $this->assign('catsOptions', $catsOptions);
            $this->assign('childCatsOptions', $childCatsOptions);
            //
            $this->assign('set', $checkdata);
            $this->assign('isProductPage', 1);
            $this->display();

        }
    }

    //商品类别下拉列表
    public function catOptions($cats, $selectedid)
    {
        $str = '';
        if ($cats)
        {
            foreach ($cats as $c)
            {
                $selected = '';
                if ($c['id'] == $selectedid)
                {
                    $selected = ' selected';
                }
                $str .= '<option value="' . $c['id'] . '"' . $selected . '>' . $c['name'] . '</option>';
            }
        }
        return $str;
    }

    public function del()
    {
        $product_model = M('Product');
        if ($this->_get('token') != session('token'))
        {
            $this->error('非法操作');
        }
        $id = $this->_get('id');
        if (IS_GET)
        {
            $where = array('id' => $id, 'token' => session('token'));
            $check = $product_model->where($where)->find();
            if ($check == false) $this->error('非法操作');

            $back = $product_model->where($wehre)->delete();
            if ($back == true)
            {
                $keyword_model = M('Keyword');
                $keyword_model->where(array('token' => session('token'), 'pid' => $id, 'module' => 'Product'))->delete();
                $this->success('操作成功', U('Product/index', array('token' => session('token'), 'dining' => $this->isDining)));
            } else
            {
                $this->error('服务器繁忙,请稍后再试', U('Product/index', array('token' => session('token'))));
            }
        }
    }

    public function orders()
    {
        $product_cart_model = M('product_cart');
        if (IS_POST)
        {
            if ($_POST['token'] != $this->_session('token'))
            {
                exit();
            }
            for ($i = 0; $i < 40; $i++)
            {
                if (isset($_POST['id_' . $i]))
                {
                    $thiCartInfo = $product_cart_model->where(array('id' => intval($_POST['id_' . $i])))->find();
                    if ($thiCartInfo['handled'])
                    {
                        $product_cart_model->where(array('id' => intval($_POST['id_' . $i])))->save(array('handled' => 0));
                    } else
                    {
                        $product_cart_model->where(array('id' => intval($_POST['id_' . $i])))->save(array('handled' => 1));
                    }
                }
            }
            $this->success('操作成功', U('Product/orders', array('token' => session('token'), 'dining' => $this->isDining)));
        } else
        {


            $where = array('token' => $this->_session('token'));
            if ($this->isDining)
            {
                $where['dining'] = 1;
            } else
            {
                $where['dining'] = 0;
            }
            $where['groupon'] = array('neq', 1);
            if (IS_POST)
            {
                $key = $this->_post('searchkey');
                if (empty($key))
                {
                    $this->error("关键词不能为空");
                }

                $where['truename|address'] = array('like', "%$key%");
                $orders = $product_cart_model->where($where)->select();
                $count = $product_cart_model->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->count();
                $Page = new Page($count, 20);
                $show = $Page->show();
            } else
            {
                if (isset($_GET['handled']))
                {
                    $where['handled'] = intval($_GET['handled']);
                }
                $count = $product_cart_model->where($where)->count();
                $Page = new Page($count, 20);
                $show = $Page->show();
                $orders = $product_cart_model->where($where)->order('time DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
            }


            $unHandledCount = $product_cart_model->where(array('token' => $this->_session('token'), 'handled' => 0))->count();
            $this->assign('unhandledCount', $unHandledCount);


            $product_cat_model = M('product_cart');
            $where = array('token' => session('token'));
            $where["sumed"] = array("neq", 1);
            $where["handled"] = array("eq", 1);
            $list = $product_cat_model->where($where)->select();


            $sumdata = M("sumdata");

            foreach ($list as $order)
            {
                $products = unserialize($order['info']);
                dump($products);

                foreach ($products as $key => $val)
                {
                    $data = array();
                    $data["pid"] = $key;
                    $data["pcount"] = $val["count"];
                    $data["pprice"] = $val["price"];
                    $data["adddate"] = date("Y-m-d", $order["time"]);
                    $data["tel"] = $order["tel"];
                    $data["truename"] = $order["truename"];
                    $sumdata->add($data);

                }
                $product_cat_model->where("id=" . $order["id"])->setField("sumed", 1);
                echo $product_cat_model->getLastSql();
            }


            $sqlx = "select pid, tel,truename, count(1) as numbers,
            sum(pprice) as tp_price,adddate  from tp_sumdata group by tel,adddate ";
            $custlist = $product_cat_model->query($sqlx);


            $info = array();
            foreach ($custlist as $k => $v)
            {
                $info[$v["tel"]] = $v["numbers"] . "次--" . $v["tp_price"] . "元";
            }


            foreach ($orders as $k => &$v)
            {
                $v["numinfo"] = $info[$v["tel"]];
            }


            $this->assign('orders', $orders);

            $this->assign('page', $show);
            $this->display();
        }
    }

    public function orderInfo()
    {
        $this->product_model = M('Product');
        $this->product_cat_model = M('Product_cat');
        $product_cart_model = M('product_cart');
        $thisOrder = $product_cart_model->where(array('id' => intval($_GET['id'])))->find();
        //检查权限
        if (strtolower($thisOrder['token']) != strtolower($this->_session('token')))
        {
            exit();
        }
        if (IS_POST)
        {
            if (intval($_POST['sent']))
            {
                $_POST['handled'] = 1;
            }
            $product_cart_model->where(array('id' => $thisOrder['id']))->save(array('sent' => intval($_POST['sent']), 'logistics' => $_POST['logistics'], 'logisticsid' => $_POST['logisticsid'], 'handled' => 1));

            $this->success('修改成功', U('Product/orderInfo', array('token' => session('token'), 'id' => $thisOrder['id'])));
        } else
        {
            //订餐信息
            $product_diningtable_model = M('product_diningtable');
            if ($thisOrder['tableid'])
            {
                $thisTable = $product_diningtable_model->where(array('id' => $thisOrder['tableid']))->find();
                $thisOrder['tableName'] = $thisTable['name'];
            }
            //
            $this->assign('thisOrder', $thisOrder);
            $carts = unserialize($thisOrder['info']);

            //
            $totalFee = 0;
            $totalCount = 0;
            $products = array();
            $ids = array();
            foreach ($carts as $k => $c)
            {
                if (is_array($c))
                {
                    $productid = $k;
                    $price = $c['price'];
                    $count = $c['count'];
                    //
                    if (!in_array($productid, $ids))
                    {
                        array_push($ids, $productid);
                    }
                    $totalFee += $price * $count;
                    $totalCount += $count;
                }
            }
            if (count($ids))
            {
                $list = $this->product_model->where(array('id' => array('in', $ids)))->select();
            }
            if ($list)
            {
                $i = 0;
                foreach ($list as $p)
                {
                    $list[$i]['count'] = $carts[$p['id']]['count'];
                    $i++;
                }
            }
            $this->assign('products', $list);
            //
            $this->assign('totalFee', $totalFee);
            $this->display();
        }
    }

    public function deleteOrder()
    {
        $product_model = M('product');
        $product_cart_model = M('product_cart');
        $product_cart_list_model = M('product_cart_list');
        $thisOrder = $product_cart_model->where(array('id' => intval($_GET['id'])))->find();
        //检查权限
        $id = $thisOrder['id'];
        if ($thisOrder['token'] != $this->_session('token'))
        {
            exit();
        }
        //
        //删除订单和订单列表
        $product_cart_model->where(array('id' => $id))->delete();
        $product_cart_list_model->where(array('cartid' => $id))->delete();
        //商品销量做相应的减少
        $carts = unserialize($thisOrder['info']);
        foreach ($carts as $k => $c)
        {
            if (is_array($c))
            {
                $productid = $k;
                $price = $c['price'];
                $count = $c['count'];
                $product_model->where(array('id' => $k))->setDec('salecount', $c['count']);
            }
        }
        $this->success('操作成功', $_SERVER['HTTP_REFERER']);
    }

    //桌台管理
    public function tables()
    {
        $product_diningtable_model = M('product_diningtable');
        if (IS_POST)
        {
            if ($_POST['token'] != $this->_session('token'))
            {
                exit();
            }
            for ($i = 0; $i < 40; $i++)
            {
                if (isset($_POST['id_' . $i]))
                {
                    $thiCartInfo = $product_cart_model->where(array('id' => intval($_POST['id_' . $i])))->find();
                    if ($thiCartInfo['handled'])
                    {
                        $product_cart_model->where(array('id' => intval($_POST['id_' . $i])))->save(array('handled' => 0));
                    } else
                    {
                        $product_cart_model->where(array('id' => intval($_POST['id_' . $i])))->save(array('handled' => 1));
                    }
                }
            }
            $this->success('操作成功', U('Product/orders', array('token' => session('token'))));
        } else
        {


            $where = array('token' => $this->_session('token'));
            if (IS_POST)
            {
                $key = $this->_post('searchkey');
                if (empty($key))
                {
                    $this->error("关键词不能为空");
                }

                $where['truename|address'] = array('like', "%$key%");
                $orders = $product_cart_model->where($where)->select();
                $count = $product_cart_model->where($where)->count();
                $Page = new Page($count, 20);
                $show = $Page->show();
            } else
            {

                $count = $product_diningtable_model->where($where)->count();
                $Page = new Page($count, 20);
                $show = $Page->show();
                $tables = $product_diningtable_model->where($where)->order('taxis ASC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
            }

            $this->assign('tables', $tables);
            $this->assign('page', $show);
            $this->display();
        }
    }

    public function tableAdd()
    {
        if (IS_POST)
        {
            $this->insert('Product_diningtable', '/tables?dining=1');
        } else
        {
            $this->display('tableSet');
        }
    }

    public function tableSet()
    {
        $product_diningtable_model = M('product_diningtable');
        $id = $this->_get('id');
        $checkdata = $product_diningtable_model->where(array('id' => $id))->find();
        if (IS_POST)
        {
            $where = array('id' => $this->_post('id'), 'token' => session('token'));
            $check = $product_diningtable_model->where($where)->find();
            if ($check == false) $this->error('非法操作');
            if ($product_diningtable_model->create())
            {
                if ($product_diningtable_model->where($where)->save($_POST))
                {
                    $this->success('修改成功', U('Product/tables', array('token' => session('token'), 'dining' => 1)));
                } else
                {
                    $this->error('操作失败');
                }
            } else
            {
                $this->error($data->getError());
            }
        } else
        {
            $this->assign('set', $checkdata);
            $this->display();

        }
    }

    public function tableDel()
    {
        if ($this->_get('token') != session('token'))
        {
            $this->error('非法操作');
        }
        $id = $this->_get('id');
        if (IS_GET)
        {
            $where = array('id' => $id, 'token' => session('token'));
            $product_diningtable_model = M('product_diningtable');
            $check = $product_diningtable_model->where($where)->find();
            if ($check == false) $this->error('非法操作');

            $back = $product_diningtable_model->where($wehre)->delete();
            if ($back == true)
            {
                $this->success('操作成功', U('Product/tables', array('token' => session('token'), 'dining' => 1)));
            } else
            {
                $this->error('服务器繁忙,请稍后再试', U('Product/tables', array('token' => session('token'), 'dining' => 1)));
            }
        }
    }

}


?>